<template>
    <d2-container>
        <!-- <el-row :gutter="20">
            <el-col :span="12">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>数据统计</span>
                    </div>
                    <div id="echart_1" style="width: 100%;height: 200px;">

                    </div>
                </el-card>
            </el-col>
            <el-col :span="12">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>数据统计</span>
                    </div>
                    <div id="echart_2" style="width: 100%;height: 200px;">

                    </div>
                </el-card>
            </el-col>
        </el-row>
        <el-row :gutter="20">
            <el-col :span="6">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>内容待定</span>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>内容待定</span>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>内容待定</span>
                    </div>
                </el-card>
            </el-col>
            <el-col :span="6">
                <el-card class="box-card">
                    <div slot="header" class="clearfix">
                        <span>内容待定</span>
                    </div>
                </el-card>
            </el-col>
        </el-row> -->
        <div class="muse_filter_box">
            <el-form ref="filtersForm" :model="form">
                <el-row :gutter="20">
                    <el-col :span="6">
                        <el-form-item>
                            <el-date-picker style="width: 100%;" v-model="form.month" value-format="yyyy-MM" type="month" placeholder="开始时间"/>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <el-button type="primary" @click="filterSearch()">查询</el-button>
                      <el-button class="muse_float_left" type="primary" @click="exportStorePointList()">导出Excel</el-button>
                    </el-col>
                </el-row>
            </el-form>
        </div>
    <div class="muse_table_box">
      <div class="muse_control_box" style="text-align: left">
        <span class="sum_up_label" style="margin-right: 10px">{{monthStr}}积分统计: </span>
        <span class="sum_up_label" style="margin-right: 10px">赠送积分: {{ giftPoint }}</span>
        <span class="sum_up_label" style="margin-right: 10px">已使用积分: {{ usePoint }}</span>
        <span class="sum_up_label" style="margin-right: 10px">兑换券购买使用积分: {{ voucherPoint }}</span>
        <span class="sum_up_label" style="margin-right: 10px">商品购买使用积分: {{ productPoint }}</span>
        <span class="sum_up_label" style="margin-right: 10px">人工调整积分: {{ manualChangePoint }}</span>
        <span class="sum_up_label" style="margin-right: 10px">至月底总积分余额: {{ canUsePoint }}</span>
      </div>



      <el-table height="58vh" :data="storePointList" style="width: 100%">
        <el-table-column prop="store" label="门店" />
        <el-table-column prop="giftPoint" label="已审核积分" />
        <el-table-column prop="usedPoint" label="已使用积分" />
        <el-table-column prop="voucherOrderPoint" label="兑换券使用积分" />
      </el-table>



    </div>
    </d2-container>
</template>

<script>
import tableMixin from "../mixins/tableMixin";
import banner from "@/components/muse/banner/banner";
import {
  monthlyPointStatistics,exportStorePointList
} from "@/api/admin";
import excel from '@/libs/util.excel.js';
  export default {
    name: 'index',
    data(){
      return{
        storePointList:[],
        giftPoint:0,
        usePoint:0,
        productPoint:0,
        voucherPoint:0,
        manualChangePoint:0,
        canUsePoint:0,
        form:{},
        monthStr:''
      }
    },
    created () {
      this.getTableData();
    },
    mounted () {
      this.draw_line()
      this.draw_bar()
    },
    methods: {
      draw_line() {
        // 基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('echart_1'))
        // 绘制图表
        myChart.setOption({
          title: {
            text: ''
          },
          tooltip : {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          legend: {
            data:[]
          },
          toolbox: {
            // feature: {
            //   saveAsImage: {}
            // }
          },
          grid: {
            top: '3%',
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis : [
            {
              type: 'category',
              data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
            }
          ],
          yAxis : [
            {
              type : 'value'
            }
          ],
          series: [{
            data: [820, 932, 901, 934, 1290, 1330, 1320],
            type: 'line'
          }]
        })
      },
      draw_bar() {
        // 基于准备好的dom，初始化echarts实例
        let myChart = this.$echarts.init(document.getElementById('echart_2'))
        // 绘制图表
        myChart.setOption({
          title: {
            text: ''
          },
          tooltip : {
            trigger: 'axis',
            axisPointer: {
              type: 'shadow',
              label: {
                backgroundColor: '#6a7985'
              }
            }
          },
          legend: {
            data:[]
          },
          toolbox: {
            // feature: {
            //   saveAsImage: {}
            // }
          },
          grid: {
            top: '3%',
            left: '3%',
            right: '4%',
            bottom: '3%',
            containLabel: true
          },
          xAxis: {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          },
          yAxis: {
            type: 'value'
          },
          series: [{
            data: [120, 200, 150, 80, 70, 110, 130],
            type: 'bar',
            showBackground: true,
            backgroundStyle: {
              color: 'rgba(220, 220, 220, 0.8)'
            }
          }]
        })
      },
    async getTableData(value, type) {
          let monthStr = '上月'
          if(this.form.month){
            monthStr = this.form.month
          }
          this.monthStr = monthStr
        let params = {}
        params = Object.assign({}, params, this.form)
      const {
        code,
        data: {
          expenseHistory,
          productPoint,
          voucherPoint,
          manualChangePoint,
          canUsePoint
        },
      } = await monthlyPointStatistics(params);
      let giftPoint = 0
      let usePoint = 0
      for(let i=0;i<expenseHistory.length;i++){
        usePoint = usePoint+expenseHistory[i].usedPoint
        giftPoint = giftPoint+expenseHistory[i].giftPoint
      }
      this.productPoint = productPoint
      this.voucherPoint = voucherPoint
      this.giftPoint = giftPoint
      this.usePoint = usePoint
      this.storePointList = expenseHistory
      this.manualChangePoint = manualChangePoint
      this.canUsePoint = canUsePoint
    },
      exportStorePointList(){
        let params = {}
        params = Object.assign({}, params, this.form)
        exportStorePointList(params).then((res) => {
          let monthStr = '上月'
          if(this.form.month){
            monthStr = this.form.month
          }
          excel.exportRaw(res.data,monthStr+'门店积分列表')
          })
      },
      filterSearch(){
      this.getTableData();
      }
    }
  }
</script>

<style lang="scss" scoped>
    .el-row {
        margin-bottom: 20px;
        &:last-child {
             margin-bottom: 0;
        }
    }
</style>
